package com.example.dworld3;

import java.util.ArrayList;
import java.util.List;

import org.Consts;
import org.Globals;
import org.json.JSONException;
import org.json.JSONObject;

import com.ydev.dworld3.R;

import CustomUI.Input_Dialog;
import CustomUI.ydDrawingCell;
import CustomUI.ydDrawingsAdapter;
import CustomUI.ydDrawingsListAdapter;
import DTO.DrawingDTO;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Drawings extends Activity {

	private LinearLayout layoutImages;
	
	RelativeLayout rrView;
	ImageView imgPicture;
	Button btnRename, btnDelete, btnEdit;

	private ProgressDialog progress;

	List<View> allViews;

	ArrayList<Integer> drawingIDs = new ArrayList<Integer>();
	ArrayList<DrawingDTO> drawings = new ArrayList<DrawingDTO>();

	GridView gridView;
	ListView listView;
	
	int lastSelectedPosition=-1;

	static final String[] MOBILE_OS = new String[] { "Android", "iOS",
			"Windows", "Blackberry" };

	ydDrawingsAdapter ydAdp;
	ydDrawingsListAdapter ydListAdp;

	getDrawingAsync2 as2 = new getDrawingAsync2();
	DrawingClassAsync dca=new DrawingClassAsync();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.drawings2);

		/*
		 * layoutImages = (LinearLayout) findViewById(R.id.linearLayoutImages);
		 * 
		 * drawingIDs = DrawingDTO.getAllDrawingsID();
		 * 
		 * gridView = (GridView) findViewById(R.id.gridView1);
		 * 
		 * gridView.setAdapter(new ydDrawingsAdapter(this, MOBILE_OS));
		 */

		progress = new ProgressDialog(Drawings.this);
		progress.setMessage("Loading...");
		progress.setIndeterminate(false);
		progress.setCancelable(false);
		// progress.show();
		// init();
		init2();
		init3();

	}

	private void init3() {
		
		rrView=(RelativeLayout) findViewById(R.id.rrView);
		imgPicture=(ImageView)findViewById(R.id.imgSmallPicture);
		btnRename=(Button) findViewById(R.id.btnRename);
		btnEdit=(Button) findViewById(R.id.btnEdit);
		btnDelete=(Button) findViewById(R.id.btnDelete);
		
		btnRename.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DrawingDTO d = (DrawingDTO) ydListAdp.getItem(lastSelectedPosition);
				Input_Dialog dialog = new Input_Dialog(ydListAdp.getContext(),
						d.getDid());
				dialog.setObjID(d.did);
				dialog.setValueText(d.getName());
				dialog.show();
				
			}
		});
		
		btnEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DrawingDTO d = (DrawingDTO) ydListAdp.getItem(lastSelectedPosition);
				Intent intent = new Intent(Drawings.this, Draw.class);
				intent.putExtra(Consts.DTO_PK, d.getDid());
				startActivity(intent);
			}
		});
		
		btnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DrawingDTO d = (DrawingDTO) ydListAdp.getItem(lastSelectedPosition);
				DrawingDTO.delete(d.did);
				//listView.removeViewAt(lastSelectedPosition);
				int x = 0;
				for (DrawingDTO i : drawings) {
					if (i.getDid() == d.did) {
						break;
					}
					x++;
				}
				drawings.remove(x);
				ydListAdp.notifyDataSetChanged();
				rrView.setVisibility(View.GONE);
			}
		});
		
		
		drawings = DrawingDTO.getAllDrawings(false);

		listView = (ListView) findViewById(R.id.listView1);
		ydListAdp = new ydDrawingsListAdapter(this, drawings);
		listView.setAdapter(ydListAdp);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				lastSelectedPosition=position;
				DrawingDTO d = (DrawingDTO) ydListAdp.getItem(position);
				Input_Dialog dialog = new Input_Dialog(ydListAdp.getContext(),
						d.getDid());
				dialog.setObjID(d.did);
				dialog.setValueText(d.getName());
				dialog.show();

			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				/*int x = parent.getId();
				Intent intent = new Intent(Drawings.this, Draw.class);
				intent.putExtra(Consts.DTO_PK, x);
				startActivity(intent);*/
				lastSelectedPosition=position;
				int did=ydListAdp.getItemDId(position);
				
				rrView.setVisibility(View.VISIBLE);
				imgPicture.setImageBitmap(DrawingDTO.getCurrentDrawing(did));
				return false;
			}

		});

		// as2.execute(drawingIDs, this);

	}

	private void init() {

		layoutImages = (LinearLayout) findViewById(R.id.linearLayoutImages);

		drawingIDs = DrawingDTO.getAllDrawingsID();
		// setupScreen();
		setupScreen2();
		return;
	}

	private void init2() {

		// layoutImages = (LinearLayout) findViewById(R.id.linearLayoutImages);

		drawingIDs = DrawingDTO.getAllDrawingsID();

		gridView = (GridView) findViewById(R.id.gridView1);
		ydAdp = new ydDrawingsAdapter(this, drawingIDs);
		gridView.setAdapter(ydAdp);

		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				// Toast.makeText(
				// getApplicationContext(), "hffh", Toast.LENGTH_SHORT).show();

			}
		});

		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				int x = parent.getId();
				Intent intent = new Intent(Drawings.this, Draw.class);
				intent.putExtra(Consts.DTO_PK, x);
				startActivity(intent);
				return false;
			}

		});
		// as2.execute(drawingIDs, this);
		return;
	}

	public void addToDrawings2(final Bitmap b, final int x) {

		new Thread(new Runnable() {

			@Override
			public void run() {

				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						View v = listView.getChildAt(x);
						((ImageView) (v.findViewById(R.id.imgSmallPicture)))
								.setImageBitmap(b);

					}
				});

			}
		}).run();

	}

	private void setupScreen2() {
		new getDrawingAsync().execute(drawingIDs, this);

	}

	public void addToDrawings(final ydDrawingCell dc) {

		new Thread(new Runnable() {

			@Override
			public void run() {

				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						layoutImages.addView(dc);
					}
				});

			}
		}).run();

	}

	private void setupScreen() {
		new Thread(new Runnable() {

			@Override
			public void run() {

				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						for (Integer i : drawingIDs) {
							ydDrawingCell dc = new ydDrawingCell(Drawings.this);
							dc.setId(i);
							dc.getDrawingID(dc.getId());
							layoutImages.addView(dc);
						}
					}
				});

			}
		}).run();
		;

	}

	public void newClicked(View v) {
		as2.cancel(true);
		Intent intent = new Intent(Drawings.this, Draw.class);
		intent.putExtra(Consts.DTO_PK, -1);
		startActivity(intent);
	}

	public void changeDrawingName(int objID, String string) {
		while(dca.getStatus()==Status.RUNNING){
			
		}
		dca.doInBackground (0, objID, string);
		int x=ydListAdp.getItemIndexWithID(objID);
		View v=listView.getChildAt(x);
		((TextView)v.findViewById(R.id.textView1)).setText(string);
	}

}

class DrawingClassAsync extends AsyncTask<Object, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Object... objs) {

		int x = (Integer) objs[0];
		if (x == 0) {
			
			int id=(Integer) objs[1];
			String name= (String) objs[2];
			
			DrawingDTO.changeName(id, name);
			
		} else if (x == 1) {
			ArrayList<Integer> drawingIDs = (ArrayList<Integer>) objs[0];
			for (Integer i : drawingIDs) {
				ydDrawingCell dc = new ydDrawingCell((Context) objs[1]);
				dc.setId(i);
				dc.getDrawingID(dc.getId());
				((Drawings) objs[1]).addToDrawings(dc);
			}
		}
		return null;

	}

	protected void onPostExecute(Double result) {

	}

	protected void onProgressUpdate(Integer... progress) {

	}
}

class getDrawingAsync2 extends AsyncTask<Object, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Object... objs) {
		ArrayList<Integer> drawingIDs = (ArrayList<Integer>) objs[0];
		int x = 0;
		for (Integer i : drawingIDs) {
			Bitmap b = DrawingDTO.getCurrentDrawing(i);
			((Drawings) objs[1]).addToDrawings2(b, x);
			x++;
		}

		return null;

	}

	protected void onPostExecute(Double result) {

	}

	protected void onProgressUpdate(Integer... progress) {

	}
}

class getDrawingAsync extends AsyncTask<Object, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Object... objs) {
		ArrayList<Integer> drawingIDs = (ArrayList<Integer>) objs[0];
		for (Integer i : drawingIDs) {
			ydDrawingCell dc = new ydDrawingCell((Context) objs[1]);
			dc.setId(i);
			dc.getDrawingID(dc.getId());
			((Drawings) objs[1]).addToDrawings(dc);
		}

		return null;

	}

	protected void onPostExecute(Double result) {

	}

	protected void onProgressUpdate(Integer... progress) {

	}
}
